package com.shiguo365.market.display.activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.github.javiersantos.bottomdialogs.BottomDialog;
import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.adapter.NotificationAdapter;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.receiver.TongZhiReceiver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NotificationActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,NotificationAdapter.OnItemClickLitener,Toolbar.OnMenuItemClickListener {

    private final String TGA="NotificationActivity";
    private List<Map> dataList = new ArrayList<Map>();
    private LinearLayoutManager mLayoutManager;
    private NotificationAdapter mAdapter;


    @BindView(R.id.notification_list_toolbar)
    protected Toolbar toolbar;

    @BindView(R.id.my_recycler_view_notificationlist)
    RecyclerView recyclerView;

    @BindView(R.id.notification_list_refresh_layout)
    protected SwipeRefreshLayout refreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        ButterKnife.bind(this);
        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));
        toolbar.inflateMenu(R.menu.del);
        toolbar.setOnMenuItemClickListener(this);

        mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        mAdapter= new NotificationAdapter(dataList);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickLitener(this);
        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);



    }

    @Override
    public void onRefresh() {
        loadData();
    }

    //数据读取
    public void loadData(){

        dataList.clear();

        SQLiteDatabase db = MyApplication.getDbHelper().getWritableDatabase();
        Cursor cursor = db.query("Notification",null,null,null,null,null,null);

        if(cursor.moveToFirst()){
            do {

                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String notificationId = cursor.getString(cursor.getColumnIndex("id"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String content = cursor.getString(cursor.getColumnIndex("content"));
                String extras = cursor.getString(cursor.getColumnIndex("extras"));
                String creatTime = cursor.getString(cursor.getColumnIndex("creatTime"));
                Map map= new HashMap();

                map.put("notificationId",notificationId);
                map.put("extras",extras);
                map.put("title",title);
                map.put("content",content);
                map.put("time",creatTime);
                dataList.add(map);

                Log.d(TGA,content.toString());

            }while (cursor.moveToNext());
        }

        Log.d(TGA,dataList.toString());

        cursor.close();

        db.close();


        mAdapter.notifyDataSetChanged();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(false);
            }
        },1000);

    }

    @Override
    public void onItemClick(View view, int position) {
        Map map =dataList.get(position);
        String title= map.get("title").toString();
        String content=map.get("content").toString();
        String extras=map.get("extras").toString();

        TongZhiReceiver tongZhiReceiver =new TongZhiReceiver(this);
        tongZhiReceiver.ShowDialog(title,content,extras);
    }

    @Override
    public void onItemLongClick(View view, int position) {

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.action_goto_del:

                AlertDialog.Builder builder=new AlertDialog.Builder(this);  //先得到构造器
                builder.setTitle("提示"); //设置标题
                builder.setMessage("是否要清理历史消息?"); //设置内容
                builder.setIcon(R.mipmap.ic_launcher);//设置图标，图片id即可
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MyApplication.getDbHelper().getWritableDatabase().execSQL("DELETE FROM Notification");
                        ToastUtils.with(MyApplication.getAppContext()).show("消息数据清理成功！");
                        onRefresh();
                        dialog.dismiss(); //关闭dialog

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();



        }
        return true;
    }
}
